﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BlobStroage
{
    public partial class Threads : Form
    {
        public Threads()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread thread1 = new Thread(Run1);
            thread1.Start();
            Thread thread2 = new Thread(Run2);
            thread2.Start();
        }

        private void Run1()
        {
            
        }
        private void Run2()
        {
            
        }

        public int GetBitSecond(string length,string size)
        {
            int bit = 0;
            if (length.Length == 0 || size.Length == 0)
            {
                throw new Exception("please check the dataSource!");
            }
            else
            {
                int time = int.Parse((((DateTime.Parse(length) - DateTime.Parse("00:00:00")).TotalMilliseconds) / 1000).ToString());
                String sizetype = size.Substring(size.Length - 2).Trim().ToLower();

                int sizeNum = 0;
                switch (sizetype)
                { 
                    case "es":
                        sizeNum = Convert.ToInt32(size.Substring(0, size.Length - 5).Trim());
                        bit = (int)Math.Round((double)sizeNum / time, 0);
                        break;
                    case "kb":
                        sizeNum = Convert.ToInt32(size.Substring(0, size.Length - 2).Trim());
                        bit = (int)Math.Round((double)sizeNum * 1024 / time, 0);
                        break;
                    case "mb":
                        sizeNum = Convert.ToInt32(size.Substring(0, size.Length - 2).Trim());
                        bit = (int)Math.Round((double)sizeNum * 1024 * 1024 / time, 0);
                        break;
                    case "gb":
                        sizeNum = Convert.ToInt32(size.Substring(0, size.Length - 2).Trim());
                        bit = (int)Math.Round((double)sizeNum * 1024 * 1024 * 1024 / time, 0);
                        break;
                }
                return bit;
            }
        }


        public String GetFileType(String filePath)
        {
            String fileType = "";
            if (!File.Exists(filePath))
            {
                throw new Exception("The file is not exist!");
            }
            else {
                String suffix = filePath.Substring(filePath.LastIndexOf('.') + 1).ToLower();
                switch(suffix){
                    case "mp3":
                        fileType = MediaType.Audio.ToString();
                        break;
                    case "mp4":
                    case "wmv":
                        fileType = MediaType.Video.ToString();
                        break;
                    case "jpg":
                    case "png":
                    case "gif":
                        fileType = MediaType.Image.ToString();
                        break;
                    default:
                        fileType = MediaType.Other.ToString();
                        break;
                }
            }
            return fileType;
        }

        public enum MediaType
        {
            Video,
            Audio,
            Image,
            Other
        }

        private void button2_Click(object sender, EventArgs e)
        {
            String path = @"C:\Users\Public\Music\Sample Music\Kalimba.mp3";
            textBox1.Text = GetFileType(path);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox2.Text = GetBitSecond("00:20:30", "50 GB").ToString();
        }
    }
}
